const express = require('express');
const apiConfig = require('./api/index')
const { PORT } = require('./config/constant')
const { jwtAuthExclued } = require('./token/index')
// 使用swagger API 文档
const swaggerInstall = require('./swagger')
//设置处理post请求参数
const bodyParser = require("body-parser");
// 定时任务
const { scheduledDeletionFolderFiles }=require('./utils/delete_files')
scheduledDeletionFolderFiles('./public/uploads')
const app = express();

swaggerInstall(app)

// app.use(bodyParser.urlencoded({
//   extended: false
// }));
app.use(bodyParser.json())

// 添加排除jwt中间件
app.use(jwtAuthExclued)

// 用于在所有HTTP请求方法的路径上加载中间件函数
app.all('*', (req, res, next) => {
    // res.header('Access-Control-Allow-Headers', 'Content-Type,Authorization');
    // res.header('Access-Control-Allow-Methods', '*');
    // res.header('Content-Type', 'application/json;charset=utf-8');
    // 设置响应头
    res.setHeader("Access-Control-Allow-Origin", "*")
    res.setHeader("Access-Control-Allow-Credentials", "true")
    next();
});

// 接口监听
apiConfig.forEach(item => {
    app[item.type](...item.params)
})
app.listen(PORT, function () {
    console.log(`服务器正在监听中${PORT}...`);
});